Page({

  cloudCallFnDemo() {
    wx.cloud.callFunction({
      name: 'toDoList'
    }).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  },
  
  onGotUserInfo(e) {
    if (e.detail.errMsg === 'getUserInfo:ok') {
      this.setData({
        showLogin: true
      })
      this.insertUserInfo(e.detail.userInfo);
    }
  },

  insertUserInfo(userInfo) {
    db.collection('user')
      .add({
        data: {
          avatarUrl: userInfo.avatarUrl,
          city: userInfo.city,
          country: userInfo.country,
          gender: userInfo.gender,
          language: userInfo.language,
          nickName: userInfo.nickName,
          province: userInfo.province
        }
      })
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.error(err)
      });
  },


  isLogin: function (_openid) {
    db.collection('user')
      .where({
        _openid
      })
      .get()
      .then(res => {
        console.log('isLogin', res.data)
        if (res.data) {
          return true
        }
      })
      .catch(err => {
        console.error(err);
      });
  }
})